package com.psp.bu.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.northend.util.NumUtil;
import com.northend.util.StringUtil;
import com.psp.bu.controller.res.BaseResult;
import com.psp.bu.controller.res.ListResult;
import com.psp.bu.controller.res.ObjectResult;
import com.psp.bu.controller.res.bean.ROrderDetailBean;
import com.psp.bu.controller.res.bean.ROrderListBean;
import com.psp.bu.controller.springmvc.req.DispatchOrderParam;
import com.psp.bu.controller.springmvc.req.GetOrderDetailParam;
import com.psp.bu.controller.springmvc.req.OrderListParam;
import com.psp.bu.service.OrderService;
import com.psp.bu.service.exception.ServiceException;
import com.psp.bu.service.impl.PageResult;

@Component
public class OrderController {
	Logger logger = Logger.getLogger(OrderController.class);

	@Autowired
	OrderService orderServiceImpl;

	/**
	 * 获取订单详情
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<ROrderDetailBean> getDetail(GetOrderDetailParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<ROrderDetailBean> result = new ObjectResult<ROrderDetailBean>();
		String token = (String) request.getAttribute("token");
		try {
			String buid = (String) request.getAttribute("userId");
			String oid = param.getOid();
			ROrderDetailBean resBean = orderServiceImpl.getDetail(buid, oid);
			result.setData(resBean);
			result.setFlag(resBean != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发生异常"));
		}
		result.setToken(token);
		return result;
	}

	/**
	 * 订单列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<ROrderListBean> list(OrderListParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<ROrderListBean> result = new ListResult<>();
		String token = (String) request.getAttribute("token");
		try {
			String buid = (String) request.getAttribute("userId");
			int page = NumUtil.toInt(param.getPage(), 0);
			int pageSize = NumUtil.toInt(param.getPageSize(), 10);
			String orderNo = StringUtil.isEmpty(param.getOrderNo()) ? null : param.getOrderNo();
			Integer state = NumUtil.toInt(param.getState(), 0);

			PageResult<ROrderListBean> resList = orderServiceImpl.getList(page, pageSize, buid, orderNo, state);

			result.setData(resList.getData());
			result.setTotalSize(resList.getCount());
			result.setPage(page);
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发生异常"));
		}
		result.setToken(token);
		return result;
	}

	/**
	 * 订单派遣
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult dispatch(DispatchOrderParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		String uid = "";
		String oid = param.getOid();
		String spid = param.getSpid();
		try {
			boolean flag = orderServiceImpl.dispatch(uid, oid, spid);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		result.setToken(param.getToken());
		return result;
	}

}
